
package shared.units.controls;

import shared.interfaces.DirectDrivable;

import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;

public class DirectTankDriver implements ActionListener {

	private InputManager inputManager;

	private DirectDrivable remoteVehicle;
	
	float steeringValue;
	float accelerationValue;
	
	public DirectTankDriver(InputManager input, DirectDrivable remoteVehicle)
	{
		this.inputManager = input;
		this.remoteVehicle = remoteVehicle;
		this.setupKeys();
	}
	
	private void setupKeys() {
		inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));
		inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));
		inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));
		inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));
		inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
		inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
		inputManager.addListener(this, "Lefts");
		inputManager.addListener(this, "Rights");
		inputManager.addListener(this, "Ups");
		inputManager.addListener(this, "Downs");
		inputManager.addListener(this, "Space");
		inputManager.addListener(this, "Reset");
	}

	@Override
	public void onAction(String binding, boolean value, float tpf) {
	     
		if (binding.equals("Lefts")) {
	            if (value) {
	                steeringValue += .5f;
	            } else {
	                steeringValue += -.5f;
	            }
	            remoteVehicle.steer(steeringValue);
	        } else if (binding.equals("Rights")) {
	            if (value) {
	                steeringValue += -.5f;
	            } else {
	                steeringValue += .5f;
	            }
	            remoteVehicle.steer(steeringValue);
	        } else if (binding.equals("Ups")) {
	            
				if (value) {
	                accelerationValue += remoteVehicle.getMaxAcceleration();
	            } else {
	                accelerationValue -= remoteVehicle.getMaxAcceleration();
	            }
				remoteVehicle.accelerate(accelerationValue);
	        } else if (binding.equals("Downs")) {
	            if (value) {
	            	remoteVehicle.brake(remoteVehicle.getMaxBrakingForce());
	            } else {
	            	remoteVehicle.brake(0f);
	            }
	        }
		System.out.println("acceleration: " + accelerationValue +"\nsteering: " + steeringValue);
	}

}
